<?php

namespace app\shop\controller\order;

use think\Controller;

class Cart extends Controller
{
    public function __construct()
    {
        // 继承父类
        parent::__construct();

        $this->ProductModel = model('Product.Product');
        $this->OrderProductModel = model('Order.Product');
        $this->OrderModel = model('Order.Order');
        $this->CartModel = model('Order.Cart');
        $this->BusinessModel = model('Business.Business');

        // 获取系统配置里面的选项
        $this->url = config('site.url') ? config('site.url') : '';

        // 判断用户是否存在
        if($this->request->isAjax())
        {
            // 接收用户id
            $this->busid = $this->request->param('busid', 0, 'trim');

            $business = $this->BusinessModel->find($this->busid);

            if(!$business)
            {
                $this->error('用户不存在');
                exit;
            }
        }
    }

    // 购物车列表
    public function index()
    {
        if($this->request->isAjax())
        {
            $cartids = $this->request->param('cartids', 0, 'trim');

            $where = [
                'busid' => $this->busid
            ];

            if($cartids)
            {
                $cartids = explode(',', $cartids);
                $where['cart.id'] = ['in', $cartids];
            }
            // 根据id 查询购物车
            $list = $this->CartModel
                ->with(['product'])
                ->where($where)
                ->select();

            $this->success('返回购物车数据', null, $list);
            exit;
        }
    }

    // 添加购物车
    public function add()
    {
        if($this->request->isAjax())
        {
            $proid = $this->request->param('proid', 0, 'trim');

            // 判断商品是否存在
            $product = $this->ProductModel->find($proid);

            if(!$product)
            {
                $this->error('商品不存在');
                exit;
            }

            // 组装数据
            $CartData = [];
            $result = false;

            // 先判断购物车是否有当前的商品记录
            $where = [
                'proid' => $proid,
                'busid' => $this->busid
            ];

            $cart = $this->CartModel->where($where)->find();

            if($cart)
            {
                // 加完后的数量
                $pronum = $cart['pronum']+1;

                $total = bcmul($cart['price'], $pronum);

                $CartData = [
                    'id' => $cart['id'],
                    'busid' => $cart['busid'],
                    'proid' => $cart['proid'],
                    'price' => $cart['price'],
                    'pronum' => $pronum,
                    'total' => $total,
                ];

                // 更新语句
                $result = $this->CartModel->validate('common/Order/Cart')->isUpdate(true)->save($CartData);
            }else
            {
                $CartData = [
                    'busid' => $this->busid,
                    'proid' => $proid,
                    'pronum' => 1,
                    'price' => $product['price'],
                    'total' => $product['price'],
                ];

                // 执行插入语句
                $result = $this->CartModel->validate('common/Order/Cart')->save($CartData);
            }

            if($result === FALSE)
            {
                $this->error($this->CartModel->getError());
                exit;
            }else
            {
                $this->success('加入购物车成功,是否跳转到购物车界面', '/order/order/cart');
                exit;
            }
        }
    }

    // 购物车编辑
    public function edit()
    {
        if($this->request->isAjax())
        {
            $id = $this->request->param('id', 0, 'trim');
            $pronum = $this->request->param('pronum', 0, 'trim');

            if($pronum <= 0)
            {
                $this->error('购物车数量不能小于0');
                exit;
            }

            // 先判断购物车记录是否存在
            $where = [
                'id' => $id,
                'busid' => $this->busid
            ];

            $cart = $this->CartModel->where($where)->find();

            if(!$cart)
            {
                $this->error('购物车记录不存在');
                exit;
            }

            // 组装更新的数据
            $data = [
                'id'=>$cart['id'],
                'busid'=>$cart['busid'],
                'proid'=>$cart['proid'],
                'pronum'=>$pronum,
                'price'=>$cart['price'],
            ];

            // 合计总价
            $data['total'] = bcmul($cart['price'], $pronum);

            // 更新
            $result = $this->CartModel->validate('common/Order/Cart')->isUpdate(true)->save($data);

            if($result === FALSE)
            {
                $this->error($this->CartModel->getError());
                exit;
            }else
            {
                $this->success('更新购物车成功');
                exit;
            }
        }
    }

    // 购物车删除
    public function del()
    {
        if($this->request->isAjax())
        {
            $cartid = $this->request->param('cartid', 0, 'trim');

            // 判断购物车记录是否存在
            $where = [
                'id' => $cartid,
                'busid' => $this->busid,
            ];

            $cart = $this->CartModel->where($where)->find();

            if(!$cart)
            {
                $this->error('购物车记录不存在');
                exit;
            }

            // 删除
            $result = $this->CartModel->destroy($cartid);

            if($result === FALSE)
            {
                $this->error('购物车删除失败');
                exit;
            }else
            {
                $this->success('购物车删除成功');
                exit;
            }
        }
    }

    // 返回购物车的数量
    public function badge()
    {
        // 查询购物车的数量
        $total = $this->CartModel->where(['busid' => $this->busid])->sum('pronum');

        $this->success('返回购物车数量', null, $total);
        exit;
    }
}
